package com.jinhua.usercenter.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinhua.usercenter.common.BusinessException;
import com.jinhua.usercenter.common.Result;
import com.jinhua.usercenter.common.ErrorCode;
import com.jinhua.usercenter.common.ResultUtils;
import com.jinhua.usercenter.contant.UserContant;
import com.jinhua.usercenter.model.User;
import com.jinhua.usercenter.model.VO.UserVO;
import com.jinhua.usercenter.model.userRequest.RegisterUserRequest;
import com.jinhua.usercenter.model.userRequest.UserLoginRequest;
import com.jinhua.usercenter.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
// todo 优化返回提示 测试抛出异常的效果
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<UserVO> doLogin(@RequestBody UserLoginRequest loginUser, HttpServletRequest request){
        if(loginUser == null){
            throw new BusinessException(ErrorCode.FAIL);
        }
        String userAccount = loginUser.getUserAccount();
        String password = loginUser.getPassword();

        if (StrUtil.hasBlank(userAccount, password)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }


        UserVO userVO = userService.doLogin(userAccount, password, request);
        if (userVO == null){
            throw new BusinessException(400, "未找到用户");
        }

        return ResultUtils.success(userVO);
    }

    @GetMapping("/current")
    public Result<UserVO> getLoginUser(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        UserVO userVO = userService.getUserVO(loginUser);
        return ResultUtils.success(userVO);
    }

    @PostMapping("/regist")
    public Result<Long> doRegist(@RequestBody RegisterUserRequest registerUser){
        if (registerUser == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        String username = registerUser.getUsername();
        String userAccount = registerUser.getUserAccount();
        String password = registerUser.getPassword();
        String checkPassword = registerUser.getCheckPassword();
        String phone = registerUser.getPhone();
        String email = registerUser.getEmail();
        //todo 增加头像功能
        String avatarUrl = "";
        Integer gender = registerUser.getGender();

        if (StrUtil.hasBlank(username, userAccount, password, checkPassword,  avatarUrl)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }

        if (gender != 0 && gender != 1){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }


        long userId = userService.registUser(username, userAccount, password, checkPassword, phone, email, avatarUrl, gender);
        if (userId < 0){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }

        return ResultUtils.success(userId);

    }

    @GetMapping("/search")
    public Result<List<UserVO>> searchUsers(String username, HttpServletRequest request){
        if (!userService.authCheck(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(username),User::getUsername,username);
        List<User> userList = userService.list(queryWrapper);
        if (userList == null){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }

        List<UserVO> userVOList = userService.getListUserVO(userList);
        return ResultUtils.success(userVOList);

    }

    @DeleteMapping()
    public Result<Boolean> deleteUser(Long id, HttpServletRequest request){
        if (!userService.authCheck(request)){
            throw new BusinessException(ErrorCode.NO_AUTH);
        }
        if (id <= 0){
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        boolean remove = userService.removeById(id);
        if (!remove){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        return ResultUtils.success(remove);

    }

    @PostMapping("/logout")
    public Result<Integer> logout(HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser != null) {
            request.getSession().removeAttribute(UserContant.USER_LOGIN_STATE);
        }
        return ResultUtils.success();
    }




}
